<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 4.2.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">

<link rel="stylesheet" href="//fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic&display=swap&subset=latin,latin-ext">
<link rel="stylesheet" href="/lib/font-awesome/css/font-awesome.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"yoursite.com","root":"/","scheme":"Pisces","version":"7.7.2","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":false,"show_result":false,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},"path":"search.xml"};
  </script>

  <meta name="description" content="Java关键词，数据类型，运算符">
<meta property="og:type" content="article">
<meta property="og:title" content="Java日拱一卒--day2">
<meta property="og:url" content="http://yoursite.com/2020/03/24/Java%E6%97%A5%E6%8B%B1%E4%B8%80%E5%8D%92-day2/index.html">
<meta property="og:site_name" content="很惭愧，就做了一点微小的工作">
<meta property="og:description" content="Java关键词，数据类型，运算符">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2020-03-24T09:27:22.000Z">
<meta property="article:modified_time" content="2020-07-02T06:08:59.950Z">
<meta property="article:author" content="文予">
<meta name="twitter:card" content="summary">

<link rel="canonical" href="http://yoursite.com/2020/03/24/Java%E6%97%A5%E6%8B%B1%E4%B8%80%E5%8D%92-day2/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>Java日拱一卒--day2 | 很惭愧，就做了一点微小的工作</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">很惭愧，就做了一点微小的工作</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
      <p class="site-subtitle" itemprop="description">念念不忘，必有回响</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-fw fa-home"></i>首页</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-fw fa-archive"></i>归档</a>

  </li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup">
        <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div id="search-result">
  <div id="no-result">
    <i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>
  </div>
</div>

    </div>
  </div>

</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>
  <div class="reading-progress-bar"></div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2020/03/24/Java%E6%97%A5%E6%8B%B1%E4%B8%80%E5%8D%92-day2/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/uploads/wenyu.png">
      <meta itemprop="name" content="文予">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="很惭愧，就做了一点微小的工作">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          Java日拱一卒--day2
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2020-03-24 17:27:22" itemprop="dateCreated datePublished" datetime="2020-03-24T17:27:22+08:00">2020-03-24</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="fa fa-calendar-check-o"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2020-07-02 14:08:59" itemprop="dateModified" datetime="2020-07-02T14:08:59+08:00">2020-07-02</time>
              </span>

          
            <div class="post-description">Java关键词，数据类型，运算符</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <h2 id="Java关键词"><a href="#Java关键词" class="headerlink" title="Java关键词"></a>Java关键词</h2><p>关键字是被<code>Java</code>语言赋予特殊含义的单词，特点为全小写，在类、变量等起名中应规避。</p>
<p>用于定义数据类型的关键字</p>
<table class="table table-bordered table-striped table-condensed">
    <tr>
        <td>class</td>
        <td>interface</td>
        <td>byte</td>
        <td>short</td>
    </tr>
    <tr>
        <td>int</td>
        <td>long</td>
        <td>float</td>
        <td>double</td>
    </tr>
    <tr>
        <td>char</td>
        <td>boolean</td>
        <td>void</td>
        <td></td>
    </tr>
</table>

<p>用于定义数据类型值的关键字</p>
<table class="table table-bordered table-striped table-condensed">
    <tr>
        <td>true</td>
        <td>false</td>
        <td>null</td>
    </tr>

<p>用于流程控制的关键字</p>
<table class="table table-bordered table-striped table-condensed">
    <tr>
        <td>if</td>
        <td>else</td>
        <td>switch</td>
        <td>case</td>
        <td>default</td>
        <td>while</td>
    </tr>
    <tr>
        <td>do</td>
        <td>for</td>
        <td>break</td>
        <td>continue</td>
        <td>return</td>
        <td></td>
    </tr>


<p>用于定义访问权限修饰符的关键字</p>
<table class="table table-bordered table-striped table-condensed">
    <tr>
        <td>private</td>
        <td>protect</td>
        <td>public</td>
        <td>default</td>
    </tr>

<p>用于定于类，函数，变量修饰符的关键字</p>
<table class="table table-bordered table-striped table-condensed">
    <tr>
        <td>abstract</td>
        <td>final</td>
        <td>static</td>
        <td>synchronized</td>
    </tr>

<p>用于定义类与类之间关系的关键字</p>
<table class="table table-bordered table-striped table-condensed">
    <tr>
        <td>extends</td>
        <td>implements</td>
    </tr>

<p>用于定义建立实例以及引用实例，判断实例的关键字</p>
<table class="table table-bordered table-striped table-condensed">
    <tr>
        <td>new</td>
        <td>this</td>
        <td>super</td>
        <td>instanceof</td>
    </tr>

<p>用于异常处理的关键字</p>
<table class="table table-bordered table-striped table-condensed">
    <tr>
        <td>try</td>
        <td>catch</td>
        <td>finally</td>
        <td>throw</td>
        <td>throws</td>
    </tr>

<p>用于包的关键字</p>
<table class="table table-bordered table-striped table-condensed">
    <tr>
        <td>package</td>
        <td>import</td>
    </tr>

<p>其他修饰符关键字</p>
<table class="table table-bordered table-striped table-condensed">
    <tr>
        <td>native</td>
        <td>stricfp</td>
        <td>transient</td>
        <td>volatile</td>
        <td>assert</td>
    </tr>

<h2 id="数据类型"><a href="#数据类型" class="headerlink" title="数据类型"></a>数据类型</h2><ol>
<li><p>基本类型，分为8种基本类型</p>
</li>
<li><p>引用类型，对象，字符串<code>String</code></p>
</li>
</ol>
<h3 id="基本类型"><a href="#基本类型" class="headerlink" title="基本类型"></a>基本类型</h3><h4 id="表示数字"><a href="#表示数字" class="headerlink" title="表示数字"></a>表示数字</h4><ol>
<li><p><code>byte</code>  1字节  2^8  -128~127</p>
</li>
<li><p><code>short</code>短整型   2字节，2^16</p>
</li>
<li><p><code>int</code>整型    4个字节 ，2^32</p>
</li>
<li><p><code>long</code>长整型  8个字节  2^64</p>
</li>
</ol>
<h4 id="表示小数"><a href="#表示小数" class="headerlink" title="表示小数"></a>表示小数</h4><p>   内存是有限稀缺资源，使用合适的数据类型，尽量减少内存占用。</p>
<pre><code>1. `float` 单精度浮点数   4个字节   2^32
2. `double` 双精度浮点型  8个字节   2^64
3. `char` 字符  1~3字节，键盘ascii码
4. `boolean` 布尔  1字节 `true`真，`false`假</code></pre><h4 id="类型范围"><a href="#类型范围" class="headerlink" title="类型范围"></a>类型范围</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">BaseType</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">       <span class="keyword">byte</span> b = <span class="number">100</span>;</span><br><span class="line">           System.out.println(<span class="string">"byte类型范围："</span>+Byte.MIN_VALUE+<span class="string">"~"</span>+Byte.MAX_VALUE);</span><br><span class="line">           System.out.println(<span class="string">"short类型范围："</span>+Short.MIN_VALUE+<span class="string">"~"</span>+Short.MAX_VALUE);</span><br><span class="line">           System.out.println(<span class="string">"int类型范围："</span>+Integer.MIN_VALUE+<span class="string">"~"</span>+Integer.MAX_VALUE);</span><br><span class="line">           System.out.println(<span class="string">"long类型范围："</span>+Long.MIN_VALUE+<span class="string">"~"</span>+Long.MAX_VALUE);</span><br><span class="line">           System.out.println(<span class="string">"float类型范围："</span>+Float.MIN_VALUE+<span class="string">"~"</span>+Float.MAX_VALUE);</span><br><span class="line">           System.out.println(<span class="string">"double类型范围："</span>+Double.MIN_VALUE+<span class="string">"~"</span>+Double.MAX_VALUE);</span><br><span class="line">           System.out.println(<span class="string">"char类型范围："</span>+Character.MIN_VALUE+<span class="string">"~"</span>+Character.MAX_VALUE);</span><br><span class="line">           <span class="comment">//acsii表开头结尾都是不可见字符</span></span><br><span class="line">           <span class="keyword">char</span> c1 = <span class="string">'a'</span>;</span><br><span class="line">           System.out.println(<span class="string">"字符a："</span>+c1);</span><br><span class="line">           <span class="keyword">char</span> c2 = <span class="number">97</span>;</span><br><span class="line">           System.out.println(<span class="string">"字符a："</span>+c2);</span><br><span class="line">       &#125;</span><br><span class="line">   &#125;</span><br></pre></td></tr></table></figure>

<blockquote>
<p>byte字节类型范围：-128~127</p>
<p>short字节类型范围：-32768~32767</p>
<p>int字节类型范围：-2147483648~2147483647</p>
<p>long字节类型范围：-9223372036854775808~9223372036854775807</p>
<p>float字节类型范围：1.4E-45~3.4028235E38</p>
<p>double字节类型范围：4.9E-324~1.7976931348623157E308</p>
<p>char字节类型范围：</p>
</blockquote>
<p>   <code>boolean</code>类型比较特殊，取值只有<code>true</code>和<code>false</code>，且在运算中遵循布尔代数。</p>
<h3 id="包装类型"><a href="#包装类型" class="headerlink" title="包装类型"></a>包装类型</h3><p><code>Java</code>为了支持传统开发方式，非面向对象方式。同时为了计算速度快，有基本类型。针对每个基本类型，对应一个包装类型，包装类型面向对象结构</p>
<p>基本类型只有数据，</p>
<p>包装类型更省事，可以使用预先提供的很多方法。</p>
<table>
<thead>
<tr>
<th align="center">基本类型</th>
<th align="center">包装类型</th>
</tr>
</thead>
<tbody><tr>
<td align="center"><code>char</code></td>
<td align="center"><code>Character</code></td>
</tr>
<tr>
<td align="center"><code>boolean</code></td>
<td align="center"><code>Boolean</code></td>
</tr>
<tr>
<td align="center"><code>byte</code></td>
<td align="center"><code>Byte</code></td>
</tr>
<tr>
<td align="center"><code>short</code></td>
<td align="center"><code>Short</code></td>
</tr>
<tr>
<td align="center"><code>int</code></td>
<td align="center"><code>Integer</code></td>
</tr>
<tr>
<td align="center"><code>long</code></td>
<td align="center"><code>Long</code></td>
</tr>
<tr>
<td align="center"><code>float</code></td>
<td align="center"><code>Float</code></td>
</tr>
<tr>
<td align="center"><code>double</code></td>
<td align="center"><code>Double</code></td>
</tr>
</tbody></table>
<p>例如</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">TestArea</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">int</span> i = <span class="number">100</span>;</span><br><span class="line">        Integer j = <span class="number">100</span>;</span><br><span class="line">        System.out.println(i);</span><br><span class="line">        System.out.println(Integer.toBinaryString(i));<span class="comment">//可以很方便的调用相关方法</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="装箱拆箱"><a href="#装箱拆箱" class="headerlink" title="装箱拆箱"></a>装箱拆箱</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">TestArea</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        <span class="comment">//把int转成Integer，把基本类型int转成包装类型Integer</span></span><br><span class="line">        <span class="keyword">int</span> a1 = <span class="number">100</span>;  </span><br><span class="line">        Integer b1 = a1;</span><br><span class="line">        System.out.println(a1);</span><br><span class="line">        System.out.println(b1);</span><br><span class="line">        <span class="comment">//a1是基本类型b1是包装类型，如何赋值</span></span><br><span class="line">        <span class="comment">//Java进行了自动装箱：Integer b1 = new Integer(a1);</span></span><br><span class="line">        Integer c1 = <span class="keyword">new</span> Integer(a1);</span><br><span class="line">        System.out.println(c1);</span><br><span class="line">        <span class="comment">//Java自动把基本类型转为包装类型</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">        <span class="comment">//把Integer转成int，包装类型转基本类型</span></span><br><span class="line">        Integer s1 = <span class="keyword">new</span> Integer(<span class="number">200</span>);<span class="comment">//声明一个包装类，值为200</span></span><br><span class="line">        <span class="keyword">int</span> s2 = s1;</span><br><span class="line"></span><br><span class="line">        <span class="comment">//真正内部执行的：</span></span><br><span class="line">        <span class="keyword">int</span> s3 = s1.intValue();</span><br><span class="line">        <span class="comment">//把上面Java自动把包装类型转为基本类型的过程称为拆箱</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="变量交换"><a href="#变量交换" class="headerlink" title="变量交换"></a>变量交换</h2><p>利用中间变量来交换两个变量的值</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Swap</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">int</span> a = <span class="number">100</span>;</span><br><span class="line">        <span class="keyword">int</span> b = <span class="number">200</span>;</span><br><span class="line">        System.out.println(<span class="string">"交换前：a="</span>+a+<span class="string">",b="</span>+b);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//利用中间变量来存储临时数据</span></span><br><span class="line">        <span class="keyword">int</span> t = a;</span><br><span class="line">        a=b;</span><br><span class="line">        b=t;</span><br><span class="line">        System.out.println(<span class="string">"交换后：a="</span>+a+<span class="string">",b="</span>+b);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="类型转换与强制类型转换"><a href="#类型转换与强制类型转换" class="headerlink" title="类型转换与强制类型转换"></a>类型转换与强制类型转换</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> org.junit.Test;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Cover</span> </span>&#123;</span><br><span class="line">    <span class="meta">@Test</span> <span class="comment">//小转大</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">smallToBig</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="comment">//byte转int</span></span><br><span class="line">        <span class="keyword">byte</span> b = <span class="number">100</span>;</span><br><span class="line">        <span class="keyword">int</span> i = b;</span><br><span class="line">        System.out.println(<span class="string">"小转大（自动转换）："</span>+i);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="meta">@Test</span>  <span class="comment">//大转小</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">bigToSmall</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">int</span> i = <span class="number">100</span>;</span><br><span class="line">        <span class="keyword">byte</span> b = (<span class="keyword">byte</span>)i;</span><br><span class="line">        <span class="comment">//把i强制转换为byte字节类型</span></span><br><span class="line">        System.out.println(<span class="string">"大转小（强制转换）："</span>+b);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="运算符"><a href="#运算符" class="headerlink" title="运算符"></a>运算符</h2><h3 id="取余运算"><a href="#取余运算" class="headerlink" title="取余运算"></a>取余运算</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">TestMod</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        <span class="comment">//给定一个值，判断是奇数还是偶数</span></span><br><span class="line">        <span class="comment">//除以2，余数为0是偶数，否则为奇数</span></span><br><span class="line">        <span class="keyword">int</span> a = <span class="number">80</span>;</span><br><span class="line">        System.out.println(<span class="string">"对2取余结果为："</span>+a%<span class="number">2</span>);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//使用if做判断</span></span><br><span class="line">        <span class="keyword">if</span>(a%<span class="number">2</span> == <span class="number">0</span>) &#123;</span><br><span class="line">            System.out.println(a+<span class="string">"是偶数"</span>);</span><br><span class="line">        &#125;<span class="keyword">else</span> &#123;</span><br><span class="line">            System.out.println(a+<span class="string">"是奇数"</span>);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="三目运算符"><a href="#三目运算符" class="headerlink" title="三目运算符"></a>三目运算符</h3><p>求两数的最大值</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Two</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        <span class="comment">//用三目运算符求两个数的最大值</span></span><br><span class="line">        <span class="keyword">int</span> x = <span class="number">1000</span>;</span><br><span class="line">        <span class="keyword">int</span> y = <span class="number">2000</span>;</span><br><span class="line">        <span class="comment">//第一个是判断条件，为真输出第二个，为假输出第三个</span></span><br><span class="line">        <span class="keyword">int</span> max = (x&gt;y)?x:y;</span><br><span class="line">        System.out.println(<span class="string">"最大值为："</span>+max);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>求三数的最大值</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Three</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        <span class="comment">//用三目运算符求两个数的最大值</span></span><br><span class="line">        <span class="keyword">int</span> x = <span class="number">1000</span>;</span><br><span class="line">        <span class="keyword">int</span> y = <span class="number">2000</span>;</span><br><span class="line">        <span class="keyword">int</span> z = <span class="number">300</span>;</span><br><span class="line">        <span class="comment">//第一个是判断条件，为真输出第二个，为假输出第三个</span></span><br><span class="line">        <span class="keyword">int</span> max = (x&gt;y)?x:y;</span><br><span class="line">        max = (z&gt;max)?z:max;</span><br><span class="line">        System.out.println(<span class="string">"最大值为："</span>+max);</span><br><span class="line">        <span class="comment">//简洁写法,三目运算符的嵌套</span></span><br><span class="line">        <span class="keyword">int</span> m = (x&gt;((y&gt;z)?y:z)?x:((y&gt;z)?y:z));</span><br><span class="line">        System.out.println(<span class="string">"最大值为："</span>+m);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="小结"><a href="#小结" class="headerlink" title="小结"></a>小结</h2><ul>
<li><ol>
<li>关键字：<code>Java</code>中已经征用，这些词不许使用（还包括<code>java</code>，<code>Test</code>这样的）</li>
</ol>
</li>
<li><ol start="2">
<li>声明变量名称，方法名，类名，标识符，这些可以又开发者自定义.一般满足以下规则：</li>
</ol>
<ul>
<li><ol>
<li>基本类型变量都是小写，<code>x,y,z,a,b</code>,单个字母，<code>x1,x2</code></li>
</ol>
</li>
<li><ol start="2">
<li>类名，方法名，复杂变量名称，遵循驼峰规则：<code>name</code>,<code>toUpperCase()</code></li>
</ol>
</li>
</ul>
</li>
<li><ol start="3">
<li>变量和常量</li>
</ol>
<ul>
<li><ol>
<li>变量就是可变化的量，可以在代码中多次赋值</li>
</ol>
</li>
<li><ol start="2">
<li>变量只能第一次赋值，不能再次赋值</li>
</ol>
</li>
</ul>
</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">final</span> <span class="keyword">double</span> PI = <span class="number">3.1415926</span>d;</span><br></pre></td></tr></table></figure>

<ul>
<li><ol start="4">
<li>8种基本类型，有点是占用内存小，执行速度快，一般变量定义尽量使用基本类型</li>
</ol>
</li>
<li><ol start="5">
<li>基本类型对应包装类型：<br>基本类型不是面向对象概念，不算对象。<br>Java中使用都是对象，Java提供基本类型转换对象类型（引用类型）<br>对应包装类型：一般情况下首字母大写：<code>byte</code>对应<code>Byte</code>，有几个特殊：<code>char</code>对应<code>Character</code>,<code>int</code>对应<code>Integer</code></li>
</ol>
</li>
<li><ol start="6">
<li><p>装箱和拆箱</p>
<p>基本类型转换包装类型，装箱</p>
</li>
</ol>
</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">int</span> i = <span class="number">10</span>;</span><br><span class="line">Integer j = i;<span class="comment">//（编译后语法：Integer j = new Integer(i); ）</span></span><br></pre></td></tr></table></figure>

<p>   包装类型转换基本类型，拆箱</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">Integer a = <span class="keyword">new</span> Integer(<span class="number">10</span>);</span><br><span class="line"><span class="keyword">int</span> b = a;<span class="comment">//(编译后语法：int b = a.intValue(); )</span></span><br></pre></td></tr></table></figure>

<ul>
<li><ol start="7">
<li>不同类型转换<br>小转大不会溢出，不会有精度问题，大转小很容易溢出，自己要清晰能否存下。</li>
</ol>
</li>
<li><ol start="8">
<li>运算符</li>
</ol>
<ul>
<li><ol>
<li>取余<code>%</code>，<code>a%b</code>，常用于判断奇偶数</li>
</ol>
</li>
<li><ol start="2">
<li>三目运算符，<code>(a&gt;b)?a:b</code></li>
</ol>
</li>
</ul>
</li>
</ul>
</table></table></table></table></table></table></table></table></table>
    </div>

    
    
    

      <footer class="post-footer">

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/2020/03/24/Java%E6%97%A5%E6%8B%B1%E4%B8%80%E5%8D%92-day1/" rel="prev" title="Java日拱一卒--day1">
      <i class="fa fa-chevron-left"></i> Java日拱一卒--day1
    </a></div>
      <div class="post-nav-item">
    <a href="/2020/03/25/Java%E6%97%A5%E6%8B%B1%E4%B8%80%E5%8D%92-day3/" rel="next" title="Java日拱一卒--day3">
      Java日拱一卒--day3 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#Java关键词"><span class="nav-number">1.</span> <span class="nav-text">Java关键词</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#数据类型"><span class="nav-number">2.</span> <span class="nav-text">数据类型</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#基本类型"><span class="nav-number">2.1.</span> <span class="nav-text">基本类型</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#表示数字"><span class="nav-number">2.1.1.</span> <span class="nav-text">表示数字</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#表示小数"><span class="nav-number">2.1.2.</span> <span class="nav-text">表示小数</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#类型范围"><span class="nav-number">2.1.3.</span> <span class="nav-text">类型范围</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#包装类型"><span class="nav-number">2.2.</span> <span class="nav-text">包装类型</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#装箱拆箱"><span class="nav-number">2.2.1.</span> <span class="nav-text">装箱拆箱</span></a></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#变量交换"><span class="nav-number">3.</span> <span class="nav-text">变量交换</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#类型转换与强制类型转换"><span class="nav-number">4.</span> <span class="nav-text">类型转换与强制类型转换</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#运算符"><span class="nav-number">5.</span> <span class="nav-text">运算符</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#取余运算"><span class="nav-number">5.1.</span> <span class="nav-text">取余运算</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#三目运算符"><span class="nav-number">5.2.</span> <span class="nav-text">三目运算符</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#小结"><span class="nav-number">6.</span> <span class="nav-text">小结</span></a></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="文予"
      src="/uploads/wenyu.png">
  <p class="site-author-name" itemprop="name">文予</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">29</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
  </nav>
</div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2020</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">文予</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://pisces.theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Pisces</a> 强力驱动
  </div>


        








      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/pisces.js"></script>


<script src="/js/next-boot.js"></script>




  




  
<script src="/js/local-search.js"></script>











<script>
if (document.querySelectorAll('pre.mermaid').length) {
  NexT.utils.getScript('//cdn.jsdelivr.net/npm/mermaid@8/dist/mermaid.min.js', () => {
    mermaid.initialize({
      theme    : 'forest',
      logLevel : 3,
      flowchart: { curve     : 'linear' },
      gantt    : { axisFormat: '%m/%d/%Y' },
      sequence : { actorMargin: 50 }
    });
  }, window.mermaid);
}
</script>


  

  

</body>
</html>
